<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true">添加分类</el-button>
        </div>
          <el-table
          stripe
          border

          :data="list"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
           prop="cate_name"
            label="分类名称"
            >
          </el-table-column>
          <el-table-column
           prop="cate_alias"
            label="分类别名">
          </el-table-column>
          <el-table-column
          label="操作">
           <template v-slot='{row}'>
            <el-button type="primary" size='mini' @click='hDialog(row)'>修改</el-button>
              <el-button type="danger" size='mini' @click="delDialog(row.id)">删除</el-button>
              </template>
          </el-table-column>
          </el-table>
    </el-card>

    <el-dialog
          title="提示"
          @closed="$refs.addRef.resetFields()"
          :close-on-click-modal='false'
          :visible.sync="dialogVisible"
          width="30%">
                    <!-- 添加的表单 -->
          <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
            </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addFn">确 定</el-button>
        </span>
    </el-dialog>

    <el-dialog
  title="修改"
  :visible.sync="dialogVisible1"
  width="30%"
 >
 <!-- 修改的表单 -->
<el-form :model="editForm" :rules="addRules" ref="editFormRef" label-width="100px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="editForm.cate_name"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="editForm.cate_alias"></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="getCate">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      list: [],
      dialogVisible: false,
      dialogVisible1: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      }
    }
  },
  created () {
    this.hSubmit()
  },
  methods: {
    async hSubmit () {
      const { data: res } = await this.$axios.get('/my/cate/list')
      // console.log(res)
      // if (res.code !== 0) return this.$message.error(res.message)
      // this.$message.success(res.message)
      this.list = res.data
    },
    addFn () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('/my/cate/add', this.addForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // this.$store.dispatch('user/getUserInfo', res.data)
        this.dialogVisible = false
        this.hSubmit()
      })
    },
    hDialog (row) {
      if (row.id === 1 || row.id === 2) return this.$message.warning('管理员禁止操作')
      // console.log(row)
      this.dialogVisible1 = true
      this.editForm = { ...row }
    },
    // 修改数据
    getCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.put('/my/cate/info', this.editForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.dialogVisible1 = false
        this.hSubmit()
      })
    },
    // 删除数据
    delDialog (id) {
      if (id === 1 || id === 2) return this.$message.warning('管理员禁止操作')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$axios.delete('/my/cate/del', { params: { id } })
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.hSubmit()
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '已取消删除'
        })
      })
    }
  }

}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
